package web;

import dao.DbOps;
import org.apache.commons.dbutils.DbUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //用户登陆功能 （判断用户输入的用户名密码和数据库存储的用户名密码是否相同）
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if(DbOps.validateUser(username, password)) {
            //设置session域 的login属性值
            HttpSession session = req.getSession();
            session.setAttribute("login", username);
            //登陆成功 跳转到首页显示登陆成功（页面、数据库表自行设计实现）
            resp.sendRedirect("success.html");
        }else {
            //登陆失败 重新登录
            resp.getWriter().write("登录失败");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
